package com.fastflow.fastflow.controller;

import com.fastflow.fastflow.bean.FlowNode;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class FlowController {

    private Logger logger = LoggerFactory.getLogger(FlowController.class);

    @RequestMapping("/saveFlow")
    public Map<String, Object> saveFlow(MultipartFile file) throws IOException, DocumentException {

        Assert.notNull(file, "流程定义文件不能为空！");

        InputStream inputStream = file.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        StringBuilder stringBuilder = new StringBuilder();
        while ((line = br.readLine()) != null) {
            stringBuilder.append(line);
            stringBuilder.append(System.lineSeparator());
        }
        br.close();
        inputStream.close();
        // 将io流，写入到text
        String text = stringBuilder.toString();
        logger.debug("text -> {}", text);

        // 使用dom工具解析符合bpm2.0的xml文件
        SAXReader reader = new SAXReader();

        Document document = reader.read(new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)));

        Element root = document.getRootElement();

        root.elements().forEach(element -> {
            System.out.println(element.getName());
        });

        // 解析 <process> 元素及其子元素
        Element processElement = root.element("process");
        if (processElement == null) {
            return new HashMap<>();
        }

        List<Element> elements = processElement.elements();
        FlowNode flowNode = new FlowNode();
        for (Element element : elements) {
            String name = element.getName();
            System.out.println(name);

            flowNode.setNodeName(name);
            if (element.attribute("id") != null) {
                flowNode.setBpmnId(element.attribute("id").getValue());
            }

            if (element.attribute("isFirst") != null) {
                flowNode.setIsFirst(element.attribute("isFirst").getValue());
            }

            Element outgoing = element.element("outgoing");
            if (outgoing != null) {
                String text1 = outgoing.getText();
                System.out.println(text1);
                if (text1 != null) {
                    flowNode.setBpmnAfterId(text1);
                }
            }

            Element incoming = element.element("incoming");
            if (incoming != null) {
                String text2 = incoming.getText();
                System.out.println(text2);
                if (text2 != null) {
                    flowNode.setBpmnBeforeId(text2);
                }
            }

            System.out.println(flowNode);
        }

        return new HashMap<>();
    }

}
